package com.school.manager.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysUser;
import com.school.manager.domaim.SysUserRole;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysUserPo;
import com.school.manager.entity.vo.SysUserVo;
import com.school.manager.enums.ExceptionEnum;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.exception.BussinessException;
import com.school.manager.service.ISysUserRoleService;
import com.school.manager.service.ISysUserService;
import com.school.manager.utils.ConvertUtil;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：登录控制器 
 * date：2021/10/182:53 下午
 */

@RestController
@Api(tags = "登录")
@RequestMapping(Constants.PROGRAM_PATH + "sys")
public class LoginController {

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private ISysUserRoleService sysUserRoleService;

	@PostMapping("login")
	@ApiOperation("用户登录")
	@ApiImplicitParam(name = "登录用户", value = "sysUserPo", dataTypeClass = SysUserPo.class)
	public ResponseEntity login(@RequestBody SysUserPo sysUserPo, HttpServletRequest request) {
		int count = sysUserService
				.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getAccount, sysUserPo.getAccount()));
		// 用户不存在
		if (count < 1) {
			throw new BussinessException(ExceptionEnum.NO_USER_EXISTS);
		}
		String password = sysUserPo.getPassword();
		String passwordEncry = DigestUtil.md5Hex(password);
		SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
				.eq(SysUser::getAccount, sysUserPo.getAccount()).eq(SysUser::getPassword, passwordEncry));
		// 用户存在
		if (ObjectUtil.isNotNull(user)) {
			if (StatusEnum.DISABLE.getCode().equals(user.getStatus())) {
				throw new BussinessException(ExceptionEnum.USER_IS_DISABLE);
			}
			SysUserVo uservo = ConvertUtil.convertObj(user, SysUserVo.class);
			// 查询用户角色
			SysUserRole userRole = sysUserRoleService
					.getOne(new LambdaQueryWrapper<SysUserRole>()
							.eq(SysUserRole::getUserId, uservo.getId())
							.eq(SysUserRole::getRoleId, sysUserPo.getRoleId())
							.eq(SysUserRole::getStatus, StatusEnum.ENABLE.getCode()));
			if (ObjectUtil.isNull(userRole)) {
				throw new BussinessException(ExceptionEnum.USER_HAS_NO_ROLE);
			}
			uservo.setRoleId(userRole.getRoleId());
			request.getSession().setAttribute(Constants.CURRENT_USER, ConvertUtil.convertObj(uservo, SysUserPo.class));
			return ResponseEnum.SUCCESS.success(uservo);
		}
		throw new BussinessException(ExceptionEnum.PASSWORD_ERROR);
	}

	@GetMapping("login-out")
	@ApiOperation("用户退出")
	public ResponseEntity loginOut(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.CURRENT_USER);
		request.getSession().setMaxInactiveInterval(1);
		return ResponseEnum.SUCCESS.success();
	}

//    @PostMapping("repwd")
//    @ApiOperation("修改密码")
//    public ResponseEntity repwd(@RequestBody UserRepwdPo sysUserPo) {
//        SysUserPo user = sysUserService.getOne(PageUtil.buildQueryWrapper(sysUserPo, SysUserPo.class));
//        if (ObjectUtil.isNull(user)) {
//            throw new BussinessException(ExceptionEnum.INIT_PASSWORD_ERROR);
//        }
//        user.setPassword(sysUserPo.getRePassword());
//        sysUserService.updateById(user);
//        return ResponseEnum.SUCCESS.success();
//    }

}
